package view.components;

import java.awt.event.ActionEvent;
import java.util.List;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextArea;

import model.turtle.Turtle;
import controller.Controller;

import view.Observer;
import view.SLogoView;

/**
 * Class for catching command typing Includes a textfield and a button Either
 * pressing "Enter" or clicking button can catch input string.
 * 
 * @author Alex Daniel, Yin Xiao
 */
@SuppressWarnings("serial")
public class CommandInput extends JPanel implements Observer {
    private JTextArea myTextArea;
    private SLogoHistoryCommands myHistory;
    private TurtleCanvas myCanvas;
    private Controller myController;
    private SLogoTabManager myTab;
    private JButton myButton;
    private static final String INPUT_SIZE = "InputSize";
    private static final String INPUT_ERROR = "InputError";

    /**
     * Set up textfield and command button
     * 
     * @param controller
     */
    public CommandInput(Controller controller, TurtleCanvas turtleCanvas,
            SLogoHistoryCommands history, SLogoTabManager tab) {
        myController = controller;
        myHistory = history;
        myCanvas = turtleCanvas;
        myTab = tab;

        List<Integer> size = SLogoView.getSize(INPUT_SIZE);
        try {
            myTextArea = new JTextArea(size.get(0), size.get(1));
        } catch (Exception e) {
            new ErrorCatcher(INPUT_ERROR);
        }

        myTextArea.setOpaque(true);

        createInputButton();

        this.add(myTextArea);
        this.add(myButton);
        setVisible(true);
    }

    /**
     * creates the enter button for the JTextArea
     */
    private void createInputButton() {
        myButton = new JButton("Enter");
        myButton.setAction(new AbstractAction("Enter") {
            public void actionPerformed(ActionEvent e) {
                myController.execute(myTextArea.getText());
                myHistory.showString(myTextArea.getText());
                myTextArea.selectAll();
            }
        });
    }

    /**
     * 
     * @return user command from typing
     */
    public String getInput() {
        return myTextArea.getText();
    }

    /**
     * displays text in JTextArea
     * 
     * @param str
     *            - the string to set
     */
    public void setInput(String str) {
        myTextArea.setText(str);
    }

    /**
     * Connect input field with given TurtleCanvas
     * 
     * @param canvas
     */
    public void setCanvas(TurtleCanvas canvas) {
        myCanvas = canvas;
    }

    /**
     * Connect input field with selected TurtleCanvas
     */
    public void setCanvas() {
        myCanvas = myTab.getTurtleCanvas();
    }

    /**
     * implement Observer
     */
    @Override
    public void update(List<Turtle> turtles) {
        setCanvas();
        myCanvas.update(turtles);
    }

}
